import { createRouter, createWebHashHistory } from 'vue-router';

const routes = [
  {
    name: 'home',
    path: '/',
    component: () => import('./view/Home'),
    meta: {
      title: '主页',
    },
  },
  {
    name: 'strategy',
    path: '/strategy',
    component: () => import('./view/strategy'),
    meta: {
      title: '策略',
    },
  },
  {
    name: 'stock',
    path: '/stock',
    component: () => import('./view/stock2'),
    meta: {
      title: '股票',
    },
  },
  {
    name: 'backtest',
    path: '/backtest',
    component: () => import('./view/backtest'),
    meta: {
      title: '回测',
    },
  },
  {
    name: 'setting',
    path: '/setting',
    component: () => import('./view/setting'),
    meta: {
      title: '设置',
    },
  },
  {
    name: 'user_info',
    path: '/manage/user_info',
    component: () => import('./view/manage/user_info'),
    meta: {
      title: '用户信息',
    },
  },
  {
    name: 'users',
    path: '/manage/users',
    component: () => import('./view/manage/users'),
    meta: {
      title: '用户管理',
    },
  },
  {
    name: 'trade_cal',
    path: '/manage/trade_cal',
    component: () => import('./view/manage/trade_cal'),
    meta: {
      title: '交易日历',
    },
  },
  {
    name: 'indicator',
    path: '/manage/indicator',
    component: () => import('./view/manage/indicator'),
    meta: {
      title: '指标学习',
    },
  },
  {
    name: 'indicator_config',
    path: '/manage/indicator_config',
    component: () => import('./view/manage/indicator_config'),
    meta: {
      title: '指标管理',
    },
  },
  {
    name: 'strategy_config',
    path: '/manage/strategy_config',
    component: () => import('./view/manage/strategy_config'),
    meta: {
      title: '策略管理',
    },
  },
  {
    name: 'notFound',
    path: '/:path(.*)+',
    redirect: {
      name: 'goods',
    },
  },
  {
    name: 'user',
    path: '/user',
    component: () => import('./view/user'),
    meta: {
      title: '会员中心',
    },
  },
  {
    name: 'cart',
    path: '/cart',
    component: () => import('./view/cart'),
    meta: {
      title: '购物车',
    },
  },
  {
    name: 'goods',
    path: '/goods',
    component: () => import('./view/goods'),
    meta: {
      title: '商品详情',
    },
  },
];

const router = createRouter({
  routes,
  history: createWebHashHistory(),
});

router.beforeEach((to, from, next) => {
  const title = to.meta && to.meta.title;
  if (title) {
    document.title = title;
  }
  next();
});

export { router };
